﻿<%@ Page Language="C#" %>

<%@ Import Namespace="IRL.Utilities" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>


<script runat="server">    
    public const int CURRENT_MODULE = 54;
    
    int proposalID;
    string projectType, researchCategory;
    string projectName, clientName, projectCode;
    float costs;
    
    Hashtable stat_lookup;
    
    protected void Page_Load(object src, EventArgs e)
    {
        Security SysAcc = new Security(CURRENT_MODULE);
        // check is authenticate
        SysAcc.Authenticate();
        
        if (Request.QueryString["pid"] == null)
        {
            // there is no query string pid
        }
        else
        {
            // load reminder count        
            // Mics.loadReminderCount(lblReminderCount);
                        
            proposalID = int.Parse(Request.QueryString["pid"]);

            if (!Page.IsPostBack)
            {
                bool isRead = false;
                // Load project information
                object connStr = ConfigurationSettings.AppSettings["sqlConn"];
                using (SqlConnection conn = new SqlConnection(connStr.ToString()))
                {
                    SqlCommand cmd = new SqlCommand("SELECT [Company_Name] AS [ClientName], p.[ProjectName], p.[ProjectCode], p.[Costs], p.[ProjectType], p.[ResearchCategory] FROM vw_Projects p INNER JOIN Tbl_Company_info c ON p.[ClientID]=c.[Company_ID] WHERE [ProposalID]=" + proposalID, conn);
                    conn.Open();
                    // get lookup                    
                    stat_lookup = Lookup.Fill("sp_Select_cms_Lookup 'milestone_status'", conn);
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        clientName = dr["ClientName"].ToString();
                        projectName = dr["ProjectName"].ToString();
                        projectCode = dr["ProjectCode"].ToString();
                        costs = Convert.ToSingle(dr["Costs"]);
                        projectType = dr["ProjectType"].ToString();
                        researchCategory = dr["ResearchCategory"].ToString();
                        isRead = true;
                    }
                    dr.Close();

                    if (isRead)
                    {
                        projectType = Lookup.GetText("sp_Select_cms_Lookup 'project_type', " + projectType + ", 1", conn);
                        researchCategory = Lookup.GetText("sp_Select_cms_Lookup 'research_category', " + researchCategory + ", 1", conn);
                    }

                    // Load milestone information
                    cmd.CommandText = "SELECT * FROM [cms_Milestones] WHERE [ProposalID]=" + proposalID + " ORDER BY [OrderID]";
                    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    grid1.DataSource = dr;
                    grid1.DataBind();
                }
            }
            
            // show billing note button
            ShowAddBillingNote();
        }
    }

    private string showAlert(string mid, int stat)
    {
        int alertCount = 0;
        if (stat != 3)// Not Completed
        {
            object connStr = ConfigurationSettings.AppSettings["sqlConn"];
            using (SqlConnection conn = new SqlConnection(connStr.ToString()))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM cms_Reminders WHERE [ReminderDate]<=GETDATE() AND [IsEnabled]=1 AND [MilestoneID]=" + mid, conn);
                alertCount = (int)cmd.ExecuteScalar();
            }
        }
        if (alertCount == 0)
        {
            return "";

        }
        else
        {
            return "<img align=\"right\" src=\"images/alert.gif\" width=\"15\" height=\"12\" alt=\"Alert\" />";
        }
    }
    
    // show task status
    private string ShowStatus(int stat)
    {
        string StrHTML = "";
        StrHTML = "<div style=\"padding-left:20px;background:url(images/status_"+ stat.ToString() +".gif) 0 no-repeat;\">"+ stat_lookup[stat.ToString()] +"</div>";
        return StrHTML;
    }
    
    // show Add Billing Note
    private void ShowAddBillingNote()
    {
        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            string StrSql = "SELECT SUM([Amount]) AS Total " +
                            "FROM cms_Projects pj INNER JOIN cms_Milestones m ON pj.[ProposalID]=m.[ProposalID] " +
                            "WHERE pj.[ProposalID]=" + proposalID;
            SqlCommand cmd = new SqlCommand(StrSql, conn);
            conn.Open();
            object total = cmd.ExecuteScalar();
            if (total != DBNull.Value)
            {
                if (costs == Convert.ToSingle(total))
                {
                    string StrScript = "<script type=\"text/javascript\">";
                    StrScript += "document.getElementById('btBillingNote').style.display=\"none\";";
                    StrScript += "<" + "/" + "script>";
                    Page.RegisterStartupScript("ShowBillingNote", StrScript);
                }
            }
        }
    }
	
	
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="Description" content="Providing the tools and expertise needed to navigate in the emerging consumer markets of Indochina." />
    <meta name="keywords" content="Research in Indochina, Research Consultants in Indochina, Marketing Research in Indochina, Marketing Research Companies in Indochina, Marketing Consultants in Indochina Research in Vietnam, Research Consultants in Vietnam, Marketing Research in Vietnam, Marketing Research Companies in Vietnam, Marketing Consultants in Vietnam Research in Cambodia, Research Consultants in Cambodia, Marketing Research in Cambodia, Marketing Research Companies in Cambodia, Marketing Consultants in Cambodia Research in Laos, Research Consultants in Laos, Marketing Research in Laos, Marketing Research Companies in Laos, Marketing Consultants in Laos" />
    <meta name="robots" content="all" />

    <title>Indochina Research</title>
    <link href="../css/global.css" rel="stylesheet" type="text/css" />
    <link href="css/cms.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" language="javascript" src="includes/menustructure.js"></script>
    
</head>
<body>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td width="765" height="540" align="center" valign="top" style="border:solid 1px #10147e;" >
			<!-- header -->
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr bgcolor="#F4F4F9">
					<td colspan="3" align="right" style="padding:10px;">
						<b class="blueL">Client Management System</b>
					</td>
				</tr>
				<tr>
					<td class="wave_l">&nbsp;</td>
					<td><img src="../images/wave_logo.jpg" width="107" height="83"></td>
					<td class="wave_r">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" style="padding:0 20px;">
						<div style="background-color:#bcb3d5; padding:5px 10px;">
							<span style="float:right">
							    <a class="blue" href="javascript:history.go(-1);">&laquo; Back</a>
							</span>
							<span class="pathmap">
								<a href="default.aspx" >CPanel</a> :: 
								<a href="default2.aspx" >Ops Files</a> :: Billing Notes
						    </span>
						</div>
						<br />
						
						
					</td>
				</tr>
				
			</table>
			
			<!-- body -->
			<form id="frmMilestones" method="post" runat="server">
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align="left" valign="top" style="padding:0 20px;">
						<table border="0" width="725" cellpadding="0" cellspacing="0">
                            <tr>
                                <td width="10"><img src="../images/spacer.gif" width="10" height="1" /></td>
                                <td>
                                    <table border="0" cellspacing="0" cellpadding="8">
                                        <tr>
                                            <td align="right">Project name:</td>
                                            <td class="blueL"><%=projectName %></td>
                                            <td>&nbsp;</td>
                                            <td align="right">Client name:</td>
                                            <td class="blueL"><%=clientName %></td>
                                        </tr>
                                        <tr>
                                            <td align="right">Type:</td>
                                            <td class="blueL"><%=projectType%></td>
                                            <td>&nbsp;</td>
                                            <td align="right">Sector:</td>
                                            <td class="blueL"><%=researchCategory %></td>
                                        </tr>
                                        <tr>
                                            <td align="right">Code:</td>
                                            <td class="blueL"><%=projectCode %></td>
                                            <td>&nbsp;</td>
                                            <td align="right">Costs:</td>
                                            <td class="blueL"><%=costs.ToString("c") %></td>
                                        </tr>
                                    </table>
                                </td>
                                <td width="20">
                                    <img src="images/spacer.gif" width="20" height="1" /></td>
                                <td nowrap>
                                    <table border="0" cellpadding="2" cellspacing="0">
                                        <tr>
                                            <td width="120" nowrap>&nbsp;
                                                <a id="btBillingNote" class="cmd" href="saveMilestone.aspx?pid=<%=proposalID %>">Add Billing note</a></td>
                                        </tr>
                                        <tr>
                                            <td width="120">
                                                <a class="cmd" href="saveProject.aspx?pid=<%=proposalID %>&lm=1">Update Project</a></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                        
                        <br />
                        <!-- Form Grid -->
                        <asp:DataGrid CssClass="data" ID="grid1" Width="100%" runat="server" CellSpacing="1" CellPadding="5"
                            BorderWidth="0" GridLines="none" AutoGenerateColumns="false" >
                            <HeaderStyle CssClass="header" />
                            <ItemStyle BackColor="#efefef" />
                            <AlternatingItemStyle BackColor="white" />
                            <Columns>
                                <asp:TemplateColumn HeaderText="No">
                                    <HeaderStyle Width="30" />
                                    <ItemTemplate>                                        
                                        <%# showAlert(DataBinder.Eval(Container.DataItem, "MilestoneID").ToString(), Convert.ToInt32(DataBinder.Eval(Container.DataItem, "Status"))) %>
                                        <%# Container.ItemIndex+1%>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Type">
                                    <HeaderStyle Width="180" />
                                    <ItemTemplate>
                                        <a href="saveMilestone.aspx?pid=<%# DataBinder.Eval(Container.DataItem, "ProposalID") %>&mid=<%# DataBinder.Eval(Container.DataItem, "MilestoneID") %>">
                                            <%# DataBinder.Eval(Container.DataItem, "MilestoneType")%>
                                            <%# DataBinder.Eval(Container.DataItem, "Note")%>
                                        </a>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Billing Date">
                                    <HeaderStyle Width="100" />
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "BillingDate", "{0:dd-MMM-yyyy}") %>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Amount">
                                    <HeaderStyle Width="80" />
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "Amount", "{0:c}")%>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Payment Date">
                                    <HeaderStyle Width="100" />
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "PaymentDate", "{0:dd-MMM-yyyy}")%>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Order" Visible="false">
                                    <HeaderStyle Width="60" />
                                    <ItemTemplate>
                                        <input name="txtOrderID" size="3" value="<%# DataBinder.Eval(Container.DataItem, "OrderID")%>"
                                            style="text-align: center;" />
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Status">
                                    <HeaderStyle Width="80" />
                                    <ItemTemplate>
                                        <%# ShowStatus((int)DataBinder.Eval(Container.DataItem, "Status"))%>
										
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                            </Columns>
                        </asp:DataGrid>
					</td>
				</tr>
			</table>
			</form>
			
		</td>
		
		<td width="6" class="rightShadow" valign="top">
			<img src="../images/white.gif" width="6" height="6" /></td>
	</tr>
	
	<!-- bottom shadow -->
	<tr>
		<td height="6" class="bottomShadow">
			<img src="../images/white.gif" width="6" height="6" /></td>
		
		<td width="6" height="6">
			<img src="../images/shadow_corner.gif" width="6" height="6" /></td>
	</tr>
</table>


</body>
</html>
